﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using System.Collections;

namespace CoreLib.Plugins
{
    public class PluginManager
    {
        public List<System.Type> PluginList=new List<Type>();
        public List<System.Type> GetPluginList<T>(string folder)
        { 
            string[] files = Directory.GetFiles(folder, "*.dll");
            PluginList.Clear();
            foreach (string file in files)
            {

                if (file.IndexOf("CoreLib")>0) // Don't load Corelib.dll
                    continue;
                try { Assembly assembly = Assembly.LoadFile(file);


                    foreach (Type type in assembly.GetTypes())
                    {

                        if (!type.IsClass || type.IsNotPublic) continue;
                            PluginList.Add(type);

                    } 
                }
                catch (Exception ex) 
                {
                    throw ex;
                }
            } 
            return PluginList; 
        }
        public object CreateInstance(string TypeName)
        {
            try
            {
                Type type;
                type = PluginList.Find(s => s.Name == TypeName);
         return Activator.CreateInstance(type, new object[] {Engine.game});
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
   
    }
}
